package openmarket.web;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class TestGetFileNames {
	public static void main(String args[]) throws FileNotFoundException,
			IOException {
		// FileWriter fw = new
		// FileWriter("/Users/macfeng/project/cloths--res.txt");
		// PrintWriter pw = new PrintWriter(fw);
		// getfiles("/Users/macfeng/project/2011-04-05",pw);
		// pw.write(replaceString("/Users/macfeng/project/cloths.txt"));
		// pw.close();
		// fw.close();
		replaceString();
	}

	public static void getfiles(String delpath, PrintWriter pw)
			throws FileNotFoundException, IOException {
		try {
			File file = new File(delpath);
			if (!file.isDirectory()) {
				System.out.println("指定不是目录!");
				// file.delete();
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File delfile = new File(delpath + "/" + filelist[i]);
					if (!delfile.isDirectory()) {
						// System.out.println(delfile.getAbsolutePath());
						if (delfile.getName().endsWith("JPG")
								|| delfile.getName().endsWith("jpg"))
							pw.println(delfile.getAbsolutePath());
					} else if (delfile.isDirectory()
							&& delfile.list().length > 0) {
						getfiles(delpath + "/" + filelist[i], pw);
					}
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("deletefile() Exception:" + e.getMessage());
		}
	}

	public static void replaceString() {
		try {
			FileReader fr = new FileReader("/Users/macfeng/project/cloths.txt");// 创建FileReader对象，用来读取字符流
			BufferedReader br = new BufferedReader(fr); // 缓冲指定文件的输入
			FileWriter fw = new FileWriter(
					"/Users/macfeng/project/clothsres4.txt");// 创建FileWriter对象，用来写入字符流
			BufferedWriter bw = new BufferedWriter(fw); // 将缓冲对文件的输出
			String myreadline; // 定义一个String类型的变量,用来每次读取一行
			String resStr;
			while (br.ready()) {
				myreadline = br.readLine();// 读取一行
				resStr = myreadline
						.replaceAll(
								"/Users/macfeng/project/2011-04-05",
								"<p align=\"center\"><img src=\"http://img.china.alibaba.com/cms/upload/2009/04/10/2011-04-05");
				String str1 = resStr.replaceAll(".jpg", ".jpg\" /></p> ");
				String str2 = str1.replaceAll(".JPG", ".JPG\" /></p> ");
				bw.write(str2+"\r\n"); // 写入文件
				// bw.newLine();
			}
			bw.flush(); // 刷新该流的缓冲
			bw.close();
			br.close();
			fw.close();
			br.close();
			fr.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
